﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

/// <summary>
/// 快递
/// </summary>
public class ExpressContext
{
    private const string host = "http://kdwlcxf.market.alicloudapi.com";
    private const string path = "/kdwlcx";
    private const string method = "GET";
    private string appcode = "1de013c086a640cdae3675972d14f0f9";

    private static ExpressContext instance = null;
    public static ExpressContext Instance
    {
        get
        {
            if (instance == null)
                instance = new ExpressContext();
            return instance;
        }
    }

    public ExpressContext()
    {
        appcode = WebConfigurationManager.AppSettings["express_appcode"];
    }

    public ExpressResponse QueryResponse(string expressNo)
    {
        var response = Query(expressNo);
        return JsonConvert.DeserializeObject<ExpressResponse>(response);
    }

    public string Query(string expressNo)
    {
        String querys = $"no={expressNo}&type=";
        String bodys = "";
        String url = host + path;
        HttpWebRequest httpRequest = null;
        HttpWebResponse httpResponse = null;

        if (0 < querys.Length)
        {
            url = url + "?" + querys;
        }

        if (host.Contains("https://"))
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
        }
        else
        {
            httpRequest = (HttpWebRequest)WebRequest.Create(url);
        }
        httpRequest.Method = method;
        httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
        if (0 < bodys.Length)
        {
            byte[] data = Encoding.UTF8.GetBytes(bodys);
            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
        }
        try
        {
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        }
        catch (WebException ex)
        {
            httpResponse = (HttpWebResponse)ex.Response;
        }

        //Console.WriteLine(httpResponse.StatusCode);
        //Console.WriteLine(httpResponse.Method);
        //Console.WriteLine(httpResponse.Headers);
        Stream st = httpResponse.GetResponseStream();
        StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
        return reader.ReadToEnd();
        //Console.WriteLine("\n");
    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }
}

public class ExpressResponse
{
    /// <summary>
    /// 状态
    /// </summary>
    public string status { get; set; }
    /// <summary>
    /// 消息
    /// </summary>
    public string msg { get; set; }
    /// <summary>
    /// 结果
    /// </summary>
    public ExpressResult result { get; set; }
}

public class ExpressResult
{
    /// <summary>
    /// 订单号
    /// </summary>
    public string number { get; set; }
    /// <summary>
    /// 快递类型
    /// </summary>
    public string type { get; set; }
    /// <summary>
    /// 快递历史
    /// </summary>
    public ExpressResultItem[] list { get; set; }
    /// <summary>
    /// 1.在途中 2.正在派件 3.已签收 4.派送失败
    /// </summary>
    public string deliverystatus { get; set; }
    /// <summary>
    /// 1.是否签收
    /// </summary>
    public string issign { get; set; }
    /// <summary>
    /// 快递名称
    /// </summary>
    public string expName { get; set; }
    /// <summary>
    /// 快递官网
    /// </summary>
    public string expSite { get; set; }
    /// <summary>
    /// 快递官方电话
    /// </summary>
    public string expPhone { get; set; }
}
public class ExpressResultItem
{
    /// <summary>
    /// 时间
    /// </summary>
    public string time { get; set; }
    /// <summary>
    /// 快递状态
    /// </summary>
    public string status { get; set; }
}

